<?php
/**
 * Magento
 *
 * NOTICE OF LICENSE
 *
 * This source file is subject to the Academic Free License (AFL 3.0)
 * that is bundled with this package in the file LICENSE_AFL.txt.
 * It is also available through the world-wide-web at this URL:
 * http://opensource.org/licenses/afl-3.0.php
 * If you did not receive a copy of the license and are unable to
 * obtain it through the world-wide-web, please send an email
 * to license@magentocommerce.com so we can send you a copy immediately.
 *
 * DISCLAIMER
 *
 * Do not edit or add to this file if you wish to upgrade Magento to newer
 * versions in the future. If you wish to customize Magento for your
 * needs please refer to http://www.magentocommerce.com for more information.
 *
 * @copyright   Copyright (c) 2014 X.commerce, Inc. (http://www.magentocommerce.com)
 * @license     http://opensource.org/licenses/afl-3.0.php  Academic Free License (AFL 3.0)
 */
?>

<div class="fieldset-wrapper">
    <div class="fieldset-wrapper-title">
        <?php $_itemsGridLabel = $this->getForcedShipmentCreate()?'Items to Invoice and Ship':'Items to Invoice'; ?>
        <span class="title"><?php echo __('%1', $_itemsGridLabel) ?></span>
    </div>
    <div class="grid">
        <table cellspacing="0" class="data">
            <thead>
                <tr class="headings">
                    <th class="col-product"><span><?php echo __('Product') ?></span></th>
                    <th class="col-price"><span><?php echo __('Price') ?></span></th>
                    <th class="col-ordered-qty"><span><?php echo __('Qty') ?></span></th>
                    <th class="col-qty-invoice"><span><?php echo __('Qty to Invoice') ?></span></th>
                    <th class="col-subtotal"><span><?php echo __('Subtotal') ?></span></th>
                    <th class="col-tax"><span><?php echo __('Tax Amount') ?></span></th>
                    <th class="col-discount"><span><?php echo __('Discount Amount') ?></span></th>
                    <th class="col-total last"><span><?php echo __('Row Total') ?></span></th>
                </tr>
            </thead>
            <?php if ($this->canEditQty()): ?>
            <tfoot>
                <tr>
                    <td colspan="2">&nbsp;</td>
                    <td colspan="3"><?php echo $this->getUpdateButtonHtml() ?></td>
                    <td colspan="3">&nbsp;</td>
                </tr>
            </tfoot>
            <?php endif; ?>
            <?php $_items = $this->getInvoice()->getAllItems() ?>
            <?php $_i=0;foreach ($_items as $_item): ?>
            <?php if ($_item->getOrderItem()->getParentItem()) continue; else $_i++; ?>
                <tbody class="<?php echo $_i%2?'even':'odd' ?>">
                    <?php echo $this->getItemHtml($_item) ?>
                    <?php echo $this->getItemExtraInfoHtml($_item->getOrderItem()) ?>
                </tbody>
            <?php endforeach; ?>
        </table>
    </div>
</div>

<?php $orderTotalBar = $this->getChildHtml('order_totalbar'); ?>

<?php if (!empty($orderTotalBar)): ?>
<div class="fieldset-wrapper">
    <?php echo $orderTotalBar; ?>
</div>
<?php endif; ?>

<div class="clearfix">
    <div class="order-comments-history">
        <div class="fieldset-wrapper">
            <div class="fieldset-wrapper-title">
                <span class="title"><?php echo __('Invoice History') ?></span>
            </div>
            <div id="history_form" class="order-history-form">
                <label class="normal" for="invoice_comment_text"><?php echo __('Invoice Comments') ?></label>
                <textarea id="invoice_comment_text" name="invoice[comment_text]" rows="3" cols="5"><?php echo $this->getInvoice()->getCommentText(); ?></textarea>
            </div>
        </div>
    </div>

    <div class="order-totals">
        <div class="fieldset-wrapper" id="invoice_totals">
            <div class="fieldset-wrapper-title">
                <span class="title"><?php echo __('Invoice Totals') ?></span>
            </div>
            <div class="order-totals-bottom">
                <?php echo $this->getChildHtml('invoice_totals') ?>
                <?php if ($this->isCaptureAllowed()): ?>
                <?php if ($this->canCapture()):?>
                    <div>
                    <!--
                      <label for="invoice_do_capture" class="normal"><?php echo __('Capture Amount') ?></label>
                      <input type="checkbox" name="invoice[do_capture]" id="invoice_do_capture" value="1" checked/>
                    -->
                      <label for="invoice_do_capture" class="normal"><?php echo __('Amount') ?></label>
                      <select name="invoice[capture_case]">
                          <option value="online"><?php echo __('Capture Online') ?></option>
                          <option value="offline"><?php echo __('Capture Offline') ?></option>
                          <option value="not_capture"><?php echo __('Not Capture') ?></option>
                      </select>
                    </div>
                <?php elseif ($this->isGatewayUsed()):?>
                    <input type="hidden" name="invoice[capture_case]" value="offline"/>
                    <div><?php echo __('The invoice will be created offline without the payment gateway.') ?></div>
                <?php endif?>
                <?php endif; ?>
                <div class="field choice field-append">
                    <input id="notify_customer" name="invoice[comment_customer_notify]" value="1" type="checkbox" />
                    <label class="normal" for="notify_customer"><?php echo __('Append Comments') ?></label>
                </div>
                <?php if ($this->canSendInvoiceEmail()): ?>
                <div class="field choice field-email">
                    <input id="send_email" name="invoice[send_email]" value="1" type="checkbox" />
                    <label class="normal" for="send_email"><?php echo __('Email Copy of Invoice') ?></label>
                </div>
                <?php endif; ?>
                <div class="actions">
                    <?php echo $this->getChildHtml('submit_button') ?>
                </div>
            </div>
        </div>
    </div>
</div>

<script type="text/javascript">
require(['prototype'], function(){

//<![CDATA[
var submitButtons = $$('.submit-button');
var updateButtons = $$('.update-button');
var enableSubmitButtons = <?php echo (int) !$this->getDisableSubmitButton() ?>;
var fields = $$('.qty-input');

updateButtons.each(function (elem) {elem.disabled=true;elem.addClassName('disabled');});

for(var i=0;i<fields.length;i++){
    fields[i].observe('change', checkButtonsRelation)
    fields[i].baseValue = fields[i].value;
}

function checkButtonsRelation() {
    var hasChanges = false;
    fields.each(function (elem) {
        if (elem.baseValue != elem.value) {
            hasChanges = true;
        }
    }.bind(this));
    if (hasChanges) {
        submitButtons.each(function (elem) {elem.disabled=true;elem.addClassName('disabled');});
        updateButtons.each(function (elem) {elem.disabled=false;elem.removeClassName('disabled');});
    }
    else {
        if (enableSubmitButtons) {
            submitButtons.each(function (elem) {elem.disabled=false;elem.removeClassName('disabled');});
        }
        updateButtons.each(function (elem) {elem.disabled=true;elem.addClassName('disabled');});
    }
}

var sendEmailCheckbox = $('send_email');
if (sendEmailCheckbox) {
    var notifyCustomerCheckbox = $('notify_customer');
    var invoiceCommentText = $('invoice_comment_text');
    Event.observe(sendEmailCheckbox, 'change', bindSendEmail);
    bindSendEmail();
}
function bindSendEmail()
{
    if (sendEmailCheckbox.checked == true) {
        notifyCustomerCheckbox.disabled = false;
        //invoiceCommentText.disabled = false;
    }
    else {
        notifyCustomerCheckbox.disabled = true;
        //invoiceCommentText.disabled = true;
    }
}

window.bindSendEmail = bindSendEmail;
window.checkButtonsRelation = checkButtonsRelation;
//]]>

});
</script>
